package models;

import java.util.LinkedList;
import java.util.List;

import siena.Column;
import siena.Generator;
import siena.Id;
import siena.Model;

/**
 * 
 * @author Dalia This table contains all the receivers of a specific
 *         notification. In case of requests, there is only one receiver. In
 *         case of offers, there is at least one receiver.
 * 
 */
public class NotificationReceiver extends Model {
	@Id(Generator.AUTO_INCREMENT)
	public long id; // row id

	@Column("notif_id")
	public long notif_id; // notification id

	@Column("receiver")
	String receiver; // phone number; map to user if they are registered

	// send message otherwise

	public NotificationReceiver() {
		// TODO Auto-generated constructor stub
	}

	public NotificationReceiver(long notifId, String receiver) {
		super();
		notif_id = notifId;
		this.receiver = receiver;
	}

	/**
	 * 
	 * @return the Notification id of the current NotificationReceiver (row)
	 */
	public long getNotif_id() {
		return notif_id;
	}

	public void setNotif_id(long notifId) {
		notif_id = notifId;
	}

	/**
	 * the phone receiver of the current NotificationReceiver (row)
	 * 
	 * @return
	 */
	public String getReceiver() {
		return receiver;
	}

	public void setReceiver(String receiver) {
		this.receiver = receiver;
	}

	/**
	 * @author Dalia
	 * @param notifId
	 * @return List of type NotificationReceiver Retrieves all receivers of a
	 *         certain notification
	 */
	public static List<NotificationReceiver> getReceiversFor(long notifId) {
		return all(NotificationReceiver.class).filter("notif_id", notifId)
				.fetch();
	}

	/**
	 * @author Dalia
	 * @param phoneReceiver
	 * @return List of type NotificationReceiver Retrieves all notifications
	 *         with a certain receiver.
	 */
	public static List<NotificationReceiver> getNotificationsWhereReceiverIs(
			String phoneReceiver) {
		return all(NotificationReceiver.class)
				.filter("receiver", phoneReceiver).fetch();
	}

	public static NotificationReceiver getNotificationReceiver(long notif_id,
			String phone) {
		return all(NotificationReceiver.class).filter("notif_id", notif_id)
				.filter("receiver", phone).get();
	}
	
	/**
	 * Delete all the receivers of a certain notification. Delete from the
	 * NotificationReceiver table the notifications with id "notif_id" (foreign
	 * key);
	 * 
	 * @param notif_id
	 * @author Dalia
	 */
	public static void deleteAllReceivers(long notif_id) {
		all(NotificationReceiver.class).filter("notif_id",notif_id).delete();
		
	}
	
	/**
	 * Delete a receiver: Delete from the NotificationReceiver table the
	 * notification with id "notif_id" (foreign key) with receiver phone;
	 * 
	 * @param notif_id
	 * @author Dalia,Gad
	 */
	public static void deleteReceiver(long notif_id, String phone) {
		// List<NotificationReceiver>rec =
		// NotificationReceiver.getReceiversFor(notif_id);
		/*
		 * for(NotificationReceiver r :rec){ r.delete(); }
		 */
		NotificationReceiver.getNotificationReceiver(notif_id, phone).delete();
	}

	/**
	 * To fetch list of phone numbers of notification
	 * @param notifId
	 *            targeted notification id
	 * @return List<String> where the elements are the phone numbers of
	 *         receivers.
	 * @author Gad
	 */
	public static List<String> getReceiversPhones(long notifId) {
		List<NotificationReceiver> receivers = getReceiversFor(notifId);
		List<String> receiverPhones = new LinkedList();
		for (NotificationReceiver nR : receivers) {
			receiverPhones.add(nR.getReceiver());
		}
		return receiverPhones;
	}

}
